package virtualstock.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "transaction", schema = "vstock")
@NamedQueries({
	@NamedQuery(name="Transaction.FindBuySharesInPortfolioByStockID", query = "SELECT SUM(a.share) FROM Transaction a WHERE a.type = 'buy' AND a.stock = :stockID AND a.portfolio = :portfolioID"),
	@NamedQuery(name="Transaction.FindSellSharesInPortfolioByStockID", query = "SELECT SUM(a.share) FROM Transaction a WHERE a.type = 'sell' AND a.stock = :stockID AND a.portfolio = :portfolioID"),
	@NamedQuery(name="Transaction.FindStocksInPortfolio", query = "SELECT DISTINCT a.stock FROM Transaction a WHERE a.portfolio = :portfolio")
})
public class Transaction implements Serializable {
	@Id
	@Column(name = "transactionID", nullable = false)
	private Long transactionID;

	@JoinColumn(name = "portfolioID")
    @ManyToOne(cascade = CascadeType.ALL)
	private Portfolio portfolio;

	@Column(name = "type")
	private String type;

	@JoinColumn(name = "stockID")
	@ManyToOne(cascade = CascadeType.ALL)
	private Stock stock;

	@Column(name = "share")
	private int share;

	@Column(name="current_price")
	private BigDecimal currentPrice;

	@Column(name = "timestamp")
	@Temporal(TemporalType.TIMESTAMP)
	private Date timestamp;

	private static final long serialVersionUID = 1L;

	public Transaction() {
		super();
	}
	
	public Transaction(
			Portfolio portfolio,
			String type,
			Stock stock,
			int share,
			BigDecimal currentPrice) {
		this.portfolio = portfolio;
		this.type = type;
		this.stock = stock;
		this.share = share;
		this.currentPrice = currentPrice;
	}

	public Long getTransactionID() {
		return this.transactionID;
	}

	public void setTransactionID(Long transactionID) {
		this.transactionID = transactionID;
	}

	public Portfolio getPortfolio() {
		return this.portfolio;
	}

	public void setPortfolio(Portfolio portfolio) {
		this.portfolio = portfolio;
	}

	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Stock getStock() {
		return this.stock;
	}

	public void setStock(Stock stock) {
		this.stock = stock;
	}

	public int getShare() {
		return this.share;
	}

	public void setShare(int share) {
		this.share = share;
	}

	public BigDecimal getCurrentPrice() {
		return this.currentPrice;
	}

	public void setCurrentPrice(BigDecimal currentPrice) {
		this.currentPrice = currentPrice;
	}

	public Date getTimestamp() {
		return this.timestamp;
	}

	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}

}
